//-------------------------------【项目】-------------------------------
//项目列表
import {
  OperatorContractObj,
  OperatorRentOrderObj,
  OwnerContractObj,
  OwnerEleOrder,
  OwnerRentOrder
} from "@/api/demoClass";
import {timeFormat, timeThan} from "@/utils/timeFormat";

export const projectList = () => {
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  projectData.forEach((item) => {
    item.stationCount = stationData.filter(station=>station.projectId === item.id).length
    if(item.ownerId) {
      item.ownerName =  ownerData.filter(owner=>owner.id === item.ownerId)[0].ownerName ?? null
    }
  })
  return projectData
}
//项目列表-by业主
export const projectListByOwner = id =>{
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  return projectData.filter(project=>project.ownerId === id)
}
//项目-byId
export const projectById = id =>{
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  return projectData.filter(project=>project.id === id)[0] ?? null
}
//新增项目
export const addProject = (project) => {
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  projectData.push(project);
  sessionStorage.setItem('projectData',JSON.stringify(projectData))
}
//修改项目
export const editProject = (project) => {
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const index = projectData.findIndex(item=>item.id === project.id)
  if(index !== -1) {
    projectData.splice(index, 1);
    projectData.splice(index,0,project);
    sessionStorage.setItem('projectData',JSON.stringify(projectData))
  }
}
//删除项目
export const delProject = (id) => {
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const index = projectData.findIndex(item=>item.id === id)
  if(index !== -1) {
    projectData.splice(index, 1);
    sessionStorage.setItem('projectData',JSON.stringify(projectData))
  }
}


//-------------------------------【站点】-------------------------------
//站点列表
export const stationList = ()=>{
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  stationData.forEach(station=>{
    if(station.projectId) {
      const project = projectData.filter(i=>i.id === station.projectId)[0] ?? null
      if(project) {
        station.projectName = `${project.projectNo}(${project.projectName})`
      }
    }
    if(station.operatorId) {
      station.operator = operatorData.filter(i=>i.id === station.operatorId)[0].operator ?? null
    }
  })
  return stationData
}
//新增站点
export const addStation = (station) => {
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  stationData.push(station);
  sessionStorage.setItem('stationData',JSON.stringify(stationData))
}
//编辑站点
export const editStation = (station) => {
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const index = stationData.findIndex(item=>item.id === station.id)
  if(index !== -1) {
    stationData.splice(index, 1);
    stationData.splice(index,0,station);
    sessionStorage.setItem('stationData',JSON.stringify(stationData))
  }
}


//-------------------------------【业主】-------------------------------
//业主列表
export const ownerList = () => {
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  ownerData.forEach((item) => {
    item.paymentCount = paymentData.filter(payment=>payment.ownerId === item.id).length
  })
  return ownerData
}
//新增业主
export const addOwner = (owner) => {
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  ownerData.push(owner);
  sessionStorage.setItem('ownerData',JSON.stringify(ownerData))
}
//编辑业主
export const editOwner = (owner) => {
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const index = ownerData.findIndex(item=>item.id === owner.id)
  if(index !== -1) {
    ownerData.splice(index, 1);
    ownerData.splice(index,0,owner);
    sessionStorage.setItem('ownerData',JSON.stringify(ownerData))
  }
}
//删除业主
export const delOwner = (id) => {
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const index = ownerData.findIndex(item=>item.id === id)
  if(index !== -1) {
    ownerData.splice(index, 1);
    sessionStorage.setItem('ownerData',JSON.stringify(ownerData))
  }
}
//-------------------------------【业主付款单位】-------------------------------
//业主付款单位列表
export const ownerPaymentList = (id) => {
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  return paymentData.filter(item=>item.ownerId === id) ?? []
}
//新增/编辑/删除业主付款单位
export const addPayment = (ownerId,payments) => {
  let paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  paymentData = paymentData.filter(item=>item.ownerId === ownerId)
  paymentData.push(...payments);
  sessionStorage.setItem('paymentData',JSON.stringify(paymentData))
}

//-------------------------------【业主合同】-------------------------------
//合同列表--正常
export const ownerContractList = ()=>{
  const resultData = []
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  projectData.forEach((item)=>{
    const contract = new OwnerContractObj({
      projectId: item.id,
      projectName: `${item.projectNo}(${item.projectName})`,
      ownerId: item.ownerId,
    })
    //业主
    if(contract.ownerId) {
      contract.ownerName = ownerData.filter(owner=>owner.id === item.ownerId)[0].ownerName ?? null
    }
    contract.status = 0
    //站点数量
    contract.stationCount = stationData.filter(station=>station.projectId === item.id).length
    //当前电费
    //该项目下有效合同--项目id、业主id、当前在合同起止期中
    const activeEleData = ownerEleData
      .filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId) && ([1,2,3].includes(i.status)) && (new Date(i.eleStart).getTime() <= Date.now() && new Date(i.eleEnd).getTime() >= Date.now()))
    if(activeEleData && activeEleData.length){
      const activeEle = activeEleData[0]
      contract.eleUnit = activeEle.eleUnit
      contract.isFloat = activeEle.isFloat
      contract.floatCount = activeEle.floatCount
    }
    //当前场租
    const activeRentData = ownerRentData
      .filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId) && ([1,2,3].includes(i.status)) && (new Date(i.rentStart).getTime() <= Date.now() && new Date(i.rentEnd).getTime() >= Date.now()))
    if(activeRentData && activeRentData.length){
      //对公
      contract.rentG = activeRentData.filter(i=>i.rentType === 0).reduce((sum,item)=>(sum + item.rentG),0) ?? null
      //对私
      contract.rentS = activeRentData.filter(i=>i.rentType === 1).reduce((sum,item)=>(sum + item.rentS),0) ?? null
      //对私年均分摊
      contract.segmentOncePrivate = activeRentData.filter(i=>i.rentType === 2).reduce((sum,item)=>(sum + item.segmentOnce),0)
      //对公年均分摊
      contract.segmentOncePublic = activeRentData.filter(i=>i.rentType === 3).reduce((sum,item)=>(sum + item.segmentOnce),0)
      //电费保证金
      contract.eleDeposit = activeRentData.filter(i=>i.rentType === 4).reduce((sum,item)=>(sum + item.eleDeposit),0)
      //场租
      contract.rentDeposit = activeRentData.filter(i=>i.rentType === 4).reduce((sum,item)=>(sum + item.rentDeposit),0)
    }
    //状态
    const eleData = ownerEleData.filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId)) ?? []
    const rentData = ownerRentData.filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId)) ?? []
    if(eleData.length || rentData.length) {
      if(eleData.some(i=>i.status === 0) || rentData.some(i=>i.status ===0)) {
        contract.status = 1
      }else if(eleData.some(i=>i.status === 1) || rentData.some(i=>i.status ===1)) {
        contract.status = 2
      }else if(eleData.every(i=>i.status !== 1) || rentData.every(i=>i.status !==1)){
        contract.status = 3
      }
    }
    resultData.push(contract)
  })
  return resultData
}
//合同列表--加待审核
export const ownerAuditContractList = ()=>{
  const resultData = []
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  projectData.forEach((item)=>{
    const contract = new OwnerContractObj({
      projectId: item.id,
      projectName: `${item.projectNo}(${item.projectName})`,
      ownerId: item.ownerId,
    })
    //业主
    if(contract.ownerId) {
      contract.ownerName = ownerData.filter(owner=>owner.id === item.ownerId)[0].ownerName ?? null
    }
    contract.status = 0
    //站点数量
    contract.stationCount = stationData.filter(station=>station.projectId === item.id).length
    //当前电费
    //该项目下有效合同--项目id、业主id、当前在合同起止期中
    const activeEleData = ownerEleData
      .filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId) && ([0,1,2,3].includes(i.status)) && (new Date(i.eleStart).getTime() <= Date.now() && new Date(i.eleEnd).getTime() >= Date.now()))
    if(activeEleData && activeEleData.length){
      const activeEle = activeEleData[0]
      contract.eleUnit = activeEle.eleUnit
      contract.isFloat = activeEle.isFloat
      contract.floatCount = activeEle.floatCount
    }
    //当前场租
    const activeRentData = ownerRentData
      .filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId) && ([0,1,2,3].includes(i.status)) && (new Date(i.rentStart).getTime() <= Date.now() && new Date(i.rentEnd).getTime() >= Date.now()))
    if(activeRentData && activeRentData.length){
      //对公
      contract.rentG = activeRentData.filter(i=>i.rentType === 0).reduce((sum,item)=>(sum + item.rentG),0) ?? null
      //对私
      contract.rentS = activeRentData.filter(i=>i.rentType === 1).reduce((sum,item)=>(sum + item.rentS),0) ?? null
      //对私年均分摊
      contract.segmentOncePrivate = activeRentData.filter(i=>i.rentType === 2).reduce((sum,item)=>(sum + item.segmentOnce),0)
      //对公年均分摊
      contract.segmentOncePublic = activeRentData.filter(i=>i.rentType === 3).reduce((sum,item)=>(sum + item.segmentOnce),0)
      //电费保证金
      contract.eleDeposit = activeRentData.filter(i=>i.rentType === 4).reduce((sum,item)=>(sum + item.eleDeposit),0)
      //场租
      contract.rentDeposit = activeRentData.filter(i=>i.rentType === 4).reduce((sum,item)=>(sum + item.rentDeposit),0)
    }
    //状态
    const eleData = ownerEleData.filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId)) ?? []
    const rentData = ownerRentData.filter(i=>(i.projectId === item.id) && (i.ownerId === item.ownerId)) ?? []
    if(eleData.length || rentData.length) {
      if(eleData.some(i=>i.status === 0) || rentData.some(i=>i.status ===0)) {
        contract.status = 1
      }else if(eleData.some(i=>i.status === 1) || rentData.some(i=>i.status ===1)) {
        contract.status = 2
      }else if(eleData.every(i=>i.status !== 1) || rentData.every(i=>i.status !==1)){
        contract.status = 3
      }
    }
    resultData.push(contract)
  })
  return resultData.filter(item=>item.status === 1) ?? []
}
//一键审核
export const agreeAll = (projectId,ownerId) => {
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  ownerEleData.forEach((item)=>{
    if(item.status === 0 && item.projectId ===projectId && item.ownerId ===ownerId) {
      item.status = 1
    }
  })
  ownerRentData.forEach((item)=>{
    if(item.status === 0 && item.projectId ===projectId && item.ownerId ===ownerId) {
      item.status = 1
    }
  })
  sessionStorage.setItem('ownerEleData',JSON.stringify(ownerEleData))
  sessionStorage.setItem('ownerRentData',JSON.stringify(ownerRentData))
}
//业主电费列表
export const ownerEleList = (projectId,ownerId)=>{
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  const activeData = ownerEleData.filter(item=>item.projectId === projectId && item.ownerId === ownerId)
  activeData.forEach(i=>{
    if(i.paymentId) {
      const payment = paymentData.filter(item=>item.id === i.paymentId)[0] ?? null
      if(payment) {
        i.paymentName = `${payment.paymentName}(${payment.account})`
      }
    }
  })
  return activeData
}
//新增业主电费
export const addOwnerEle = (ownerEle) => {
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  //同项目同业主合约
  const activeData = ownerEleData.filter(item=>item.projectId === ownerEle.projectId && item.ownerId === ownerEle.ownerId)
  //判断周期冲突
  const flag = activeData
    .filter(item=>item.status !== 4)
    .some(item=>{
      return (timeThan(item.eleStart,ownerEle.eleStart) && timeThan(ownerEle.eleStart,item.eleEnd)) || (timeThan(item.eleStart,ownerEle.eleEnd) && timeThan(ownerEle.eleEnd,item.eleEnd))
    })
  if(flag) {
    return '该合同与已有合同的周期冲突，请重新选择'
  }else {
    if(timeThan(ownerEle.eleStart,null) && timeThan(null,ownerEle.eleEnd)) {
      ownerEle.status = 0
    }
    if(timeThan(ownerEle.eleEnd,null)) {
      ownerEle.status = 2
    }
    if(timeThan(null,ownerEle.eleStart)) {
      ownerEle.status = 3
    }
    ownerEleData.push(ownerEle);
    sessionStorage.setItem('ownerEleData',JSON.stringify(ownerEleData))
  }
}
//编辑业主电费
export const editOwnerEle = (ownerEle) => {
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  //同项目同业主合约
  const activeData = ownerEleData.filter(item=>item.projectId === ownerEle.projectId && item.ownerId === ownerEle.ownerId && item.id !== ownerEle.id )
  const flag = activeData
    .filter(item=>item.status !== 4)
    .some(item=>{
      return (timeThan(item.eleStart,ownerEle.eleStart) && timeThan(ownerEle.eleStart,item.eleEnd)) || (timeThan(item.eleStart,ownerEle.eleEnd) && timeThan(ownerEle.eleEnd,item.eleEnd))
    })
  if(flag) {
    return '该合同与历史合同的周期冲突，请重新选择'
  }else {
    if(timeThan(ownerEle.eleStart,null) && timeThan(null,ownerEle.eleEnd)) {
      ownerEle.status = 0
    }
    if(timeThan(ownerEle.eleEnd,null)) {
      ownerEle.status = 2
    }
    if(timeThan(null,ownerEle.eleStart)) {
      ownerEle.status = 3
    }
    const index = ownerEleData.findIndex(item=>item.id === ownerEle.id)
    if(index !== -1) {
      ownerEleData.splice(index, 1);
      ownerEleData.splice(index,0,ownerEle);
      sessionStorage.setItem('ownerEleData',JSON.stringify(ownerEleData))
    }
  }
}
//业主场租列表
export const ownerRentList = (projectId,ownerId)=>{
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  const activeData = ownerRentData.filter(item=>item.projectId === projectId && item.ownerId === ownerId)
  activeData.forEach(i=>{
    if(i.paymentId) {
      const payment = paymentData.filter(item=>item.id === i.paymentId)[0] ?? null
      if(payment) {
        i.paymentName = `${payment.paymentName}(${payment.account})`
      }
    }
  })
  return activeData
}
//新增业主场租
export const addOwnerRent = (ownerRent) => {
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  //同项目同业主合约
  const activeData = ownerRentData.filter(item=>item.projectId === ownerRent.projectId && item.ownerId === ownerRent.ownerId)
  //是否有待审核
  const flag1 = activeData.some(item=>item.status === 0) && timeThan(ownerRent.rentStart,null) && timeThan(null,ownerRent.rentEnd)
  // const flag1 = false
  if(flag1) {
    return '存在未审核的合同，请先审核'
  }else {
    if(timeThan(ownerRent.rentStart,null) && timeThan(null,ownerRent.rentEnd)) {
      ownerRent.status = 0
    }
    if(timeThan(ownerRent.rentEnd,null)) {
      ownerRent.status = 2
    }
    if(timeThan(null,ownerRent.rentStart)) {
      ownerRent.status = 3
    }
    ownerRentData.push(ownerRent);
    sessionStorage.setItem('ownerRentData',JSON.stringify(ownerRentData))
  }
}
//编辑业主场租
export const editOwnerRent = (ownerRent) => {
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  const index = ownerRentData.findIndex(item=>item.id === ownerRent.id)
  if(index !== -1) {
    ownerRentData.splice(index, 1);
    ownerRentData.splice(index,0,ownerRent);
    sessionStorage.setItem('ownerRentData',JSON.stringify(ownerRentData))
  }
}
//自动拆分场租订单
export const autoCreateRentOrder = (ownerRent)=>{
  const ownerRentData = JSON.parse(sessionStorage.getItem('ownerRentData')) ?? [];
  const contract = ownerRentData.filter(item=>item.id === ownerRent.id)[0] ?? null
  const resultData = []
  if(contract && contract.status ===0) {
    //租金G、租金S订单
    if(contract.rentType === 0 || contract.rentType === 1){
      //订单数量
      //除数
      const rentCycles = [
        {label:0,value:1},
        {label:1,value:2},
        {label:2,value:3},
        {label:3,value:5},
        {label:4,value:10}
      ]
      const divisor = rentCycles.filter(i=>i.label === contract.rentCycle)[0].value ?? null
      const diffs = new Date(contract.rentEnd).getTime() - new Date(contract.rentStart).getTime()
      const commercial = Math.ceil(diffs / (divisor * 365 * 24 * 3600000))
      for(let i = 1; i <= commercial; i++) {
        const rentOrder = new OwnerRentOrder({
          projectId: contract.projectId,
          ownerId: contract.ownerId,
          paymentId: contract.paymentId,
          contractId: contract.id,
          orderType: contract.rentType,
        })
        //开始日期
        if(i===0) {
          rentOrder.orderStart = contract.rentStart
        }else {
          rentOrder.orderStart = timeFormat(new Date(contract.rentStart).getTime() + ((i -1) * (365 * 24 * 3600000)))
        }
        //结束日期
        if(i===commercial) {
          rentOrder.orderEnd = contract.rentEnd
        }else {
          rentOrder.orderEnd = timeFormat(new Date(contract.rentStart).getTime() + (i * (365 * 24 * 3600000)) - (24 * 3600000))
        }
        //金额
        rentOrder.orderPrice = contract.rentType === 0 ? contract.rentG * divisor : contract.rentS * divisor
        if(diffs % divisor !== 0 && i === commercial ) {
          rentOrder.orderPrice = contract.rentType === 0 ? Math.round((contract.rentG / 365) * ((diffs % (divisor * 365 * 24 * 3600000))/(24 * 3600000))) : Math.round((contract.rentS / 365) * ((diffs % (divisor * 365 * 24 * 3600000))/(24 * 3600000)))
        }
        rentOrder.remark = contract.rentType === 0 ? '租金G订单--自动生成' : '租金S订单--自动生成'
        rentOrder.status = 0
        resultData.push(rentOrder)
      }
    }
    //对私处理、对公处理订单
    if(contract.rentType === 2 || contract.rentType === 3) {
      //订单数量
      //除数
      const rentCycles = [
        {label:0,value:1},
        {label:1,value:2},
        {label:2,value:3},
        {label:3,value:4},
        {label:4,value:5}
      ]
      const orderCount = rentCycles.filter(i=>i.label === contract.segmentCount)[0].value ?? null
      for(let i = 1; i <= orderCount; i++) {
        const rentOrder = new OwnerRentOrder({
          projectId: contract.projectId,
          ownerId: contract.ownerId,
          paymentId: contract.paymentId,
          contractId: contract.id,
          orderType: contract.rentType,
        })
        //开始日期
        if(i===0) {
          rentOrder.orderStart = `${new Date(contract.rentStart).getFullYear()}-01-01`
        }else {
          rentOrder.orderStart = `${new Date(new Date(contract.rentStart).getTime() + ((i -1) * (365 * 24 * 3600000))).getFullYear()}-01-01`
        }
        //结束日期
        rentOrder.orderEnd = `${new Date(new Date(contract.rentStart).getTime() + ((i -1) * (365 * 24 * 3600000))).getFullYear()}-12-31`
        //金额
        rentOrder.orderPrice = contract.segmentOnce
        rentOrder.remark = contract.rentType === 2 ? '对私年均分摊订单--自动生成' : '对公年均分摊订单--自动生成'
        rentOrder.status = 0
        resultData.push(rentOrder)
      }
    }
    //保证金订单
    if(contract.rentType === 4) {
      const rentOrder = new OwnerRentOrder({
        projectId: contract.projectId,
        ownerId: contract.ownerId,
        paymentId: contract.paymentId,
        contractId: contract.id,
        orderType: contract.rentType,
      })
      rentOrder.orderStart = contract.rentStart
      rentOrder.orderEnd = contract.rentEnd
      //金额
      rentOrder.orderPrice = contract.eleDeposit + contract.rentDeposit
      rentOrder.remark = `保证金订单--自动生成(电费保证金：${contract.eleDeposit}，场租保证金：${contract.rentDeposit})`
      rentOrder.status = 0
      resultData.push(rentOrder)
    }
  }
  return resultData
}
//业主场租订单列表
export const ownerRentOrderList = ()=>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  ownerRentOrderData.forEach(item=>{
    //项目
    if(item.projectId) {
      const project = projectData.filter(i=>i.id === item.projectId)[0] ?? null;
      if(project) {
        item.projectName = `${project.projectNo}(${project.projectName})`
      }
    }
    //业主
    if(item.ownerId) {
      item.ownerName = ownerData.filter(i=>i.id === item.ownerId)[0].ownerName ?? null;
    }
    //付款单位
    if(item.paymentId) {
      const payment = paymentData.filter(i=>i.id === item.paymentId)[0] ?? null;
      if(payment) {
        item.paymentName = payment.paymentName
        item.bank = payment.bank
        item.account = payment.account
      }
    }
  })
  return ownerRentOrderData
}
//业主场租订单列表---未支付
export const ownerRentOrderNoPayList = (contractId)=>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  ownerRentOrderData.forEach(item=>{
    //项目
    if(item.projectId) {
      const project = projectData.filter(i=>i.id === item.projectId)[0] ?? null;
      if(project) {
        item.projectName = `${project.projectNo}(${project.projectName})`
      }
    }
    //业主
    if(item.ownerId) {
      item.ownerName = ownerData.filter(i=>i.id === item.ownerId)[0].ownerName ?? null;
    }
    //付款单位
    if(item.paymentId) {
      const payment = paymentData.filter(i=>i.id === item.paymentId)[0] ?? null;
      if(payment) {
        item.paymentName = `${payment.paymentName}(${payment.account})`
      }
    }
  })
  return ownerRentOrderData.filter(i=>i.contractId === contractId)
}
//新增业主场租订单
export const addOwnerRentOrder = (ownerRentOrders)=>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  ownerRentOrderData.push(...ownerRentOrders)
  sessionStorage.setItem('ownerRentOrderData',JSON.stringify(ownerRentOrderData))
}
//编辑业主场租订单
export const editOwnerRentOrder = (ownerRentOrder)=>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  const index = ownerRentOrderData.findIndex(item=>item.id === ownerRentOrder.id)
  if(index !== -1) {
    ownerRentOrderData.splice(index, 1);
    ownerRentOrderData.splice(index,0,ownerRentOrder);
    sessionStorage.setItem('ownerRentOrderData',JSON.stringify(ownerRentOrderData))
  }
}
//批量作废业主场租订单
export const cancelOwnerRentOrders = (ownerRentOrders) =>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  ownerRentOrders.forEach(i=>{
    const index = ownerRentOrderData.findIndex(ownerRentOrder=>ownerRentOrder.id === i.id)
    if(index !== -1) {
      ownerRentOrderData.splice(index, 1);
      ownerRentOrderData.splice(index,0,i);
    }
  })
  sessionStorage.setItem('ownerRentOrderData',JSON.stringify(ownerRentOrderData))
}
//一键支付
export const toPay = (orderId)=>{
  const ownerRentOrderData = JSON.parse(sessionStorage.getItem('ownerRentOrderData')) ?? [];
  const index = ownerRentOrderData.findIndex(item=>item.id === orderId)
  if(index !== -1) {
    ownerRentOrderData[index].realPrice = ownerRentOrderData[index].orderPrice
    ownerRentOrderData[index].status = 2
  }
  sessionStorage.setItem('ownerRentOrderData',JSON.stringify(ownerRentOrderData))
}

//-------------------------------【电表】-------------------------------
//电表列表
export const materList = ()=>{
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  meterData.forEach(meter=>{
    if(meter.stationId) {
      //所属站点
      const station = stationData.filter(i=>i.id === meter.stationId)[0] ?? null
      if(station) {
        meter.stationName = `${station.stationNo}(${station.planName})`
        meter.operatorName = station.operator
        //所属项目
        const project = projectData.filter(i=> i.id === station.projectId)[0] ?? null
        if(project) {
          meter.projectId = project.id
          meter.projectName = `${project.projectNo}(${project.projectName})`
          if(project.ownerId) {
            meter.ownerId = ownerData.filter(i=>i.id === project.ownerId)[0].id ?? null
            meter.ownerName = ownerData.filter(i=>i.id === project.ownerId)[0].ownerName ?? null
          }
        }
      }
    }
  })
  return meterData;
}
//新增电表
export const addMeter = (meter)=>{
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  meterData.push(meter)
  sessionStorage.setItem('meterData',JSON.stringify(meterData))
}
//编辑电表
export const editMeter = (meter)=>{
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const index = meterData.findIndex(item=>item.id === meter.id)
  if(index !== -1) {
    meterData.splice(index, 1);
    meterData.splice(index,0,meter);
    sessionStorage.setItem('meterData',JSON.stringify(meterData))
  }
}
//抄表记录列表(byMeterId)
export const sendRecordListByMeterId = id =>{
  const sendRecordData = JSON.parse(sessionStorage.getItem('sendRecordData')) ?? [];
  const activeData = sendRecordData.filter(i=>i.meterId === id)
  activeData.forEach((item)=>{
    item.canCancel = !activeData.filter(i => i.status === 0 && i.issueNo === item.issueNo + 1).length && !(item.status === 1);
  })
  return activeData.sort((a,b)=>b.issueNo - a.issueNo)
}
//新增抄表记录
export const addSendRecord = (record) =>{
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const sendRecordData = JSON.parse(sessionStorage.getItem('sendRecordData')) ?? [];
  const index = meterData.findIndex(item=>item.id === record.meterId)
  if(index !== -1) {
    const issueTotal = sendRecordData.filter(i=>i.meterId === record.meterId && i.status === 0).length ?? 0
    //最近抄表
    meterData[index].sendRecent = record.sendTime
    //期数
    record.issueNo = issueTotal + 1
    //用电量
    record.useDegree = record.meterDegree - meterData[index].currentDegree
    //电表度数
    meterData[index].currentDegree = record.meterDegree
    sendRecordData.push(record)
    sessionStorage.setItem('sendRecordData',JSON.stringify(sendRecordData))
    sessionStorage.setItem('meterData',JSON.stringify(meterData))
  }
}
//作废抄表记录
export const delSendRecord = record =>{
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const sendRecordData = JSON.parse(sessionStorage.getItem('sendRecordData')) ?? [];
  const index = meterData.findIndex(item=>item.id === record.meterId)
  if(index !== -1) {
    //最近抄表
    meterData[index].sendRecent = record.issueNo > 1 ? sendRecordData.filter(i=>i.meterId === record.meterId && i.status === 0 && i.issueNo === record.issueNo - 1)[0].sendTime : null
    //电表度数
    meterData[index].currentDegree = record.meterDegree - record.useDegree
    sessionStorage.setItem('meterData',JSON.stringify(meterData))
  }
  const index1 = sendRecordData.findIndex(item=>item.id === record.id)
  if(index1 !== -1) {
    sendRecordData.splice(index1, 1);
    sendRecordData.splice(index1,0,record)
    sessionStorage.setItem('sendRecordData',JSON.stringify(sendRecordData))
  }
}
//-------------------------------【业主电费】-------------------------------
//根据电表查询单价
export const searchOwnerUnit = (meter)=>{
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  return ownerEleData.filter(item=>item.projectId === meter.projectId && item.ownerId === meter.ownerId && ![0,4].includes(item.status)) ?? false
}
//业主电费订单列表
export const ownerEleOrderList = ()=>{
  const ownerEleOrderData = JSON.parse(sessionStorage.getItem('ownerEleOrderData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  ownerEleOrderData.forEach(order=>{
    //所属站点
    const station = stationData.filter(i=>i.id === order.stationId)[0] ?? null
    if(station) {
      order.stationName = `${station.stationNo}(${station.planName})`
    }
    //所属项目
    const project = projectData.filter(i=>i.id === order.projectId)[0] ?? null
    if(project) {
      order.projectName = `${project.projectNo}(${project.projectName})`
    }
    //业主
    const owner = ownerData.filter(i=>i.id === order.ownerId)[0] ?? null
    if(owner) {
      order.ownerName = owner.ownerName
    }
    //付款单位
    const payment = paymentData.filter(i=>i.id === order.paymentId)[0] ?? null
    if(payment) {
      order.paymentName = payment.paymentName
      order.bank = payment.bank
      order.account = payment.account
    }
  })
  return ownerEleOrderData
}
//业主电费订单列表--非标确认
export const ownerTargetEleOrderList = ()=>{
  const ownerEleOrderData = JSON.parse(sessionStorage.getItem('ownerEleOrderData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerData = JSON.parse(sessionStorage.getItem('ownerData')) ?? [];
  const paymentData = JSON.parse(sessionStorage.getItem('paymentData')) ?? [];
  ownerEleOrderData.forEach(order=>{
    //所属站点
    const station = stationData.filter(i=>i.id === order.stationId)[0] ?? null
    if(station) {
      order.stationName = `${station.stationNo}(${station.planName})`
    }
    //所属项目
    const project = projectData.filter(i=>i.id === order.projectId)[0] ?? null
    if(project) {
      order.projectName = `${project.projectNo}(${project.projectName})`
    }
    //业主
    const owner = ownerData.filter(i=>i.id === order.ownerId)[0] ?? null
    if(owner) {
      order.ownerName = owner.ownerName
    }
    //付款单位
    const payment = paymentData.filter(i=>i.id === order.paymentId)[0] ?? null
    if(payment) {
      order.paymentName = payment.paymentName
      order.bank = payment.bank
      order.account = payment.account
    }
  })
  return ownerEleOrderData.filter(i=>i.status === 1)
}
//批量生成电费订单
export const createOwnerEleOrders = (carData)=>{
  const ordersData = []
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const projectData = JSON.parse(sessionStorage.getItem('projectData')) ?? [];
  const ownerEleData = JSON.parse(sessionStorage.getItem('ownerEleData')) ?? [];
  const ownerEleOrderData = JSON.parse(sessionStorage.getItem('ownerEleOrderData')) ?? [];
  let searchFlag = true
  carData.forEach(carItem=>{
    const eleOrder = new OwnerEleOrder({meterId:carItem.meterId})
      if(carItem.meterId) {
        //所属站点,项目,业主
        const meter = meterData.filter(i=>i.id === carItem.meterId)[0] ?? null
        eleOrder.meterId = meter.id ?? null
        eleOrder.meterNo = meter.meterNo ?? null
        eleOrder.stationId = meter.stationId ?? null
        //所属项目
        const station = stationData.filter(i=>i.id === meter.stationId)[0] ?? null
        if(station) {
          eleOrder.projectId = station.projectId ?? null
          const project = projectData.filter(i=>i.id === station.projectId)[0] ?? null
          if(project) {
            eleOrder.ownerId = project.ownerId ?? null
          }
        }
        //查询当前业主合同,项目id、业主id、周期、状态
        const contracts = ownerEleData.filter(i=>i.projectId === eleOrder.projectId && i.ownerId === eleOrder.ownerId && timeThan(i.eleStart,null) && timeThan(null,i.eleEnd) && ![0,4].includes(i.eleStart) && ![0,4].includes(i.status))
        if(contracts && contracts.length) {
          const currentContract = contracts[0]
          eleOrder.contractId = currentContract.id
          eleOrder.paymentId = currentContract.paymentId
          eleOrder.startDegree = carItem.startDegree
          eleOrder.endDegree = carItem.endDegree
          eleOrder.startTime = carItem.startTime
          eleOrder.endTime = carItem.endTime
          eleOrder.payDegree = carItem.endDegree - carItem.startDegree
          eleOrder.eleUnit = currentContract.eleUnit
          eleOrder.eleTotal = (carItem.endDegree - carItem.startDegree) * currentContract.eleUnit
          eleOrder.isFloat = currentContract.isFloat
          eleOrder.maxUnit = currentContract.isFloat === 1 ? (currentContract.eleUnit *(1+ (currentContract.floatCount / 100))) : currentContract.eleUnit
          eleOrder.status = 0
          ordersData.push(eleOrder)
        }else {
          return searchFlag = false
        }
      }else {
        return searchFlag = false
      }
  })
  if(!searchFlag) {
    return false
  }
  //订单s
  if(ordersData.length) {
    ordersData.forEach(order=>{
      console.log(order)
      const index = meterData.findIndex(i=>i.id === order.meterId)
      console.log(index)
      if(index !== -1) {
        meterData[index].ownerDone = order.endDegree - (meterData[index].initDegree ?? 0)
        meterData[index].ownerRecent = order.endTime
        console.log(meterData[index])
      }
    })
    sessionStorage.setItem('meterData',JSON.stringify(meterData))
    ownerEleOrderData.push(...ordersData)
    sessionStorage.setItem('ownerEleOrderData',JSON.stringify(ownerEleOrderData))
    return true
  }else {
    return false
  }
}
//查询电表订单是否已结清
export const isDoneByMeterId = meterId => {
  const ownerEleOrderData = JSON.parse(sessionStorage.getItem('ownerEleOrderData')) ?? [];
  const activeData = ownerEleOrderData.filter(i=>i.meterId === meterId)
  return activeData.every(i=>i.status === 3 || i.status === 6)
}
//作废电费订单
export const delOwnerEleOrder = ownerEleOrder => {
  const meterData = JSON.parse(sessionStorage.getItem('meterData')) ?? [];
  const ownerEleOrderData = JSON.parse(sessionStorage.getItem('ownerEleOrderData')) ?? [];
  //回退电表已结算度数
  const index = meterData.findIndex(i=>i.id === ownerEleOrder.meterId)
  if(index !== -1) {
    meterData[index].ownerDone = ownerEleOrder.startDegree - (meterData[index].initDegree ?? 0)
    meterData[index].ownerRecent = ownerEleOrder.startTime !== meterData[index].initTime ? meterData[index].startTime : null
  }
  sessionStorage.setItem('meterData',JSON.stringify(meterData))
  //修改订单
  const index1 = ownerEleOrderData.findIndex(i=>i.id === ownerEleOrder.id)
  if(index !== -1) {
    console.log(1)
    ownerEleOrderData.splice(index1,1)
    ownerEleOrderData.splice(index1,0,ownerEleOrder)
  }
  sessionStorage.setItem('ownerEleOrderData',JSON.stringify(ownerEleOrderData))
}
//-------------------------------【运营商】-------------------------------
//运营商列表
export const operatorList = ()=>{
  return JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
}
//新增运营商
export const addOperator = (operator)=>{
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  operatorData.push(operator)
  sessionStorage.setItem('operatorData',JSON.stringify(operatorData))
}
//编辑运营商
export const editOperator= (operator)=>{
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  const index = operatorData.findIndex(item=>item.id === operator.id)
  if(index !== -1) {
    operatorData.splice(index, 1);
    operatorData.splice(index,0,operator);
    sessionStorage.setItem('operatorData',JSON.stringify(operatorData))
  }
}
//删除运营商
export const delOperator= (id)=>{
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  const index = operatorData.findIndex(item=>item.id === id)
  if(index !== -1) {
    operatorData.splice(index, 1);
    sessionStorage.setItem('operatorData',JSON.stringify(operatorData))
  }
}
//-------------------------------【运营商合同】-------------------------------
//运营商合同
export const operatorContractList = ()=>{
  const resultData = []
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  stationData.forEach(station=>{
    if([3,4].includes(station.status)) {
      const resultItem = new OperatorContractObj({
        stationId: station.id,
        stationName: `${station.stationNo}(${station.planName})`,
        operatorId: station.operatorId,
        operator: operatorData.filter(i=>i.id === station.operatorId)[0].operator ?? null,
        signingName: station.signingName,
        status: 0
      })
      resultData.push(resultItem)
    }
  })
  return resultData
}
//运营商电费
//电费列表
export const operatorEleList = (stationId,operatorId)=>{
  const operatorEleData = JSON.parse(sessionStorage.getItem('operatorEleData')) ?? [];
  return operatorEleData.filter(i=>i.stationId === stationId && i.operatorId === operatorId)
}
//新增
export const addOperatorEle = (operatorEle)=>{
  const operatorEleData = JSON.parse(sessionStorage.getItem('operatorEleData')) ?? [];
  const activeData = operatorEleData.filter(i=>i.stationId === operatorEle.stationId && i.operatorId === operatorEle.operatorId && i.status !== 3);
  const flag = activeData.some(item=>{
    return (item.eleStart <= operatorEle.eleStart && item.eleEnd >= operatorEle.eleStart) || (item.eleStart <= operatorEle.eleEnd && item.eleEnd >= operatorEle.eleEnd)
  })
  if(flag) {
    return '该合同与历史合同的周期冲突，请重新选择'
  }else {
    operatorEleData.push(operatorEle)
    sessionStorage.setItem('operatorEleData',JSON.stringify(operatorEleData))
  }
}
//编辑
export const editOperatorEle = (operatorEle)=>{
  const operatorEleData = JSON.parse(sessionStorage.getItem('operatorEleData')) ?? [];
  const index = operatorEleData.findIndex(item=>item.id === operatorEle.id)
  if(index !== -1) {
    operatorEleData.splice(index, 1);
    operatorEleData.splice(index, 0,operatorEle);
  }
  sessionStorage.setItem('operatorEleData',JSON.stringify(operatorEleData))
}
//运营商场租
//场租列表
export const operatorRentList = (stationId,operatorId)=>{
  const operatorRentData = JSON.parse(sessionStorage.getItem('operatorRentData')) ?? [];
  return operatorRentData.filter(i=>i.stationId === stationId && i.operatorId === operatorId)
}
//新增
export const addOperatorRent = (operatorRent)=>{
  const operatorRentData = JSON.parse(sessionStorage.getItem('operatorRentData')) ?? [];
  const activeData = operatorRentData.filter(i=>i.stationId === operatorRent.stationId && i.operatorId === operatorRent.operatorId && i.status !== 3);
  const flag = activeData.some(item=>{
    return (item.rentStart <= operatorRent.rentStart && item.rentEnd >= operatorRent.rentStart) || (item.rentStart <= operatorRent.rentEnd && item.rentEnd >= operatorRent.rentEnd)
  })
  if(flag) {
    return '该合同与历史合同的周期冲突，请重新选择'
  }else {
    operatorRentData.push(operatorRent)
    sessionStorage.setItem('operatorRentData',JSON.stringify(operatorRentData))
  }
}
//编辑
export const editOperatorRent = (operatorRent)=>{
  const operatorRentData = JSON.parse(sessionStorage.getItem('operatorRentData')) ?? [];
  const index = operatorRentData.findIndex(item=>item.id === operatorRent.id)
  if(index !== -1) {
    operatorRentData.splice(index, 1);
    operatorRentData.splice(index, 0,operatorRent);
  }
  sessionStorage.setItem('operatorRentData',JSON.stringify(operatorRentData))
}
//自动拆分场租订单
export const autoCreateRentOrder2 = (operatorRent)=>{
  const resultData = []
  //除数
  const rentCycles = [
    {label:0,value:1},
    {label:1,value:2},
    {label:2,value:3},
    {label:3,value:5},
    {label:4,value:10}
  ]
  const divisor = rentCycles.filter(i=>i.label === operatorRent.rentCycle)[0].value ?? null
  const diffs = new Date(operatorRent.rentEnd).getTime() - new Date(operatorRent.rentStart).getTime()
  const commercial = Math.ceil(diffs / (divisor * 365 * 24 * 3600000))
  for(let i = 1; i <= commercial; i++) {
    const rentOrder = new OperatorRentOrderObj({
      stationId: operatorRent.stationId,
      operatorId: operatorRent.operatorId,
    })
    //开始日期
    if(i===0) {
      rentOrder.orderStart = operatorRent.rentStart
    }else {
      rentOrder.orderStart = timeFormat(new Date(operatorRent.rentStart).getTime() + ((i -1) * (365 * 24 * 3600000)))
    }
    //结束日期
    if(i===commercial) {
      rentOrder.orderEnd = operatorRent.rentEnd
    }else {
      rentOrder.orderEnd = timeFormat(new Date(operatorRent.rentStart).getTime() + (i * (365 * 24 * 3600000)) - (24 * 3600000))
    }
    //金额
    rentOrder.orderPrice = operatorRent.rentOnce * divisor
    if(diffs % divisor !== 0 && i === commercial ) {
      rentOrder.orderPrice = Math.round((operatorRent.rentOnce / 365) * ((diffs % (divisor * 365 * 24 * 3600000))/(24 * 3600000)))
    }
    rentOrder.remark = '年付租金--自动生成'
    rentOrder.status = 0
    resultData.push(rentOrder)
  }
  return resultData
}
//场租订单列表
export const operatorRentOrderList = ()=> {
  const operatorRentOrderData = JSON.parse(sessionStorage.getItem('operatorRentOrderData')) ?? [];
  const operatorRentData = JSON.parse(sessionStorage.getItem('operatorRentData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  operatorRentOrderData.forEach(item=>{
    if(item.stationId) {
      const station = stationData.filter(i=>i.id === item.stationId)[0] ?? null
      if(station) {
        item.stationName = `${station.stationNo}(${station.planName})`
      }
    }
    if(item.operatorId) {
      const operator = operatorData.filter(i=>i.id === item.operatorId)[0] ?? null
      if(operator) {
        item.operator = operator.operator
      }
    }
    if(item.contractId) {
      const contract = operatorRentData.filter(i=>i.id === item.contractId)[0] ?? null
      if(contract) {
        item.rentSigningUnit = contract.rentSigningUnit
        item.rentInvoiceType = contract.rentInvoiceType
        item.rentTax = contract.rentTax
        if(!item.rentInvoiceContent) item.rentInvoiceContent = contract.rentInvoiceContent
      }
    }
  })
  return operatorRentOrderData
}
//新增场租订单
export const addOperatorRentOrder = (operatorRentOrder)=> {
  const operatorRentOrderData = JSON.parse(sessionStorage.getItem('operatorRentOrderData')) ?? [];
  operatorRentOrderData.push(operatorRentOrder)
  sessionStorage.setItem('operatorRentOrderData',JSON.stringify(operatorRentOrderData))
}
//编辑场租订单
export const editOperatorRentOrder = (operatorRentOrder)=> {
  const operatorRentOrderData = JSON.parse(sessionStorage.getItem('operatorRentOrderData')) ?? [];
  const index = operatorRentOrderData.findIndex(item=>item.id === operatorRentOrder.id)
  if(index !== -1) {
    operatorRentOrderData.splice(index, 1);
    operatorRentOrderData.splice(index, 0,operatorRentOrder);
  }
  sessionStorage.setItem('operatorRentOrderData',JSON.stringify(operatorRentOrderData))
}
//-------------------------------【发票管理】-------------------------------
//开票申请
//列表
export const takeRentInvoiceList = ()=>{
  const takeRentInvoiceData = JSON.parse(sessionStorage.getItem('takeRentInvoiceData')) ?? [];
  const stationData = JSON.parse(sessionStorage.getItem('stationData')) ?? [];
  const operatorData = JSON.parse(sessionStorage.getItem('operatorData')) ?? [];
  takeRentInvoiceData.forEach(item=>{
    if(item.stationId) {
      const station = stationData.filter(i=>i.id === item.stationId)[0] ?? null
      if(station) {
        item.stationName = `${station.stationNo}(${station.planName})`
        item.signingName = station.signingName
        item.isNew = station.isNew
        item.area = station.area
        item.address = station.address
      }
    }
    if(item.operatorId) {
      item.operator = operatorData.filter(i=>i.id === item.operatorId)[0].operator ?? null
    }
  })
  console.log(takeRentInvoiceData)
  return takeRentInvoiceData
}
//新增
export const addTakeRentInvoice = (takeRentInvoice) =>{
  const takeRentInvoiceData = JSON.parse(sessionStorage.getItem('takeRentInvoiceData')) ?? [];
  const operatorRentOrderData = JSON.parse(sessionStorage.getItem('operatorRentOrderData')) ?? [];
  operatorRentOrderData.forEach(item=>{
    if(takeRentInvoice.orderId.includes(item.id)) {
      item.status = 2
    }
  })
  sessionStorage.setItem('operatorRentOrderData',JSON.stringify(operatorRentOrderData))
  takeRentInvoiceData.push(takeRentInvoice)
  sessionStorage.setItem('takeRentInvoiceData',JSON.stringify(takeRentInvoiceData))
}
//编辑
export const editTakeRentInvoice = (takeRentInvoice) =>{
  const takeRentInvoiceData = JSON.parse(sessionStorage.getItem('takeRentInvoiceData')) ?? [];
  const index = takeRentInvoiceData.findIndex(item=>item.id === takeRentInvoice.id)
  if(index !== -1) {
    takeRentInvoiceData.splice(index, 1);
    takeRentInvoiceData.splice(index,0,takeRentInvoice)
  }
  sessionStorage.setItem('takeRentInvoiceData',JSON.stringify(takeRentInvoiceData))
}
